﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.Security.Encryption;

namespace YAPACS.RisServer.Controllers;

[Authorize]
public class ExportListController : AbpController
{
    private readonly IStringEncryptionService _stringEncryptionService;

    public ExportListController(IStringEncryptionService stringEncryptionService)
    {
        _stringEncryptionService = stringEncryptionService;
    }

    [HttpGet("/api/export-list/{key}")]
    public IActionResult GetExportListFile(string key, [FromQuery] string fileName)
    {
        key = _stringEncryptionService.Decrypt(key);
        var tempFile = Path.Combine(Path.GetTempPath(), key);
        if (Path.Exists(tempFile))
        {
            fileName = string.IsNullOrWhiteSpace(fileName) ? "检查列表" : fileName;
            try
            {
                var bytes = System.IO.File.ReadAllBytes(tempFile);
                return File(bytes, "application/octet-stream", $"{fileName}.xlsx");
            }
            finally
            {
                System.IO.File.Delete(tempFile);
            }
        }
        return NotFound();
    }
}
